import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createTransport, Transport } from 'nodemailer';

@Injectable()
export class EmailService {
  transport: Transport;
  constructor(private readonly configService: ConfigService) {
    this.transport = createTransport({
      host: this.configService.get('EMAIL_HOST'),
      port: this.configService.get('EMAIL_PORT'),
      secure: true,
      name: this.configService.get('EMAIL_NAME'),
      version: '1.0.0',
      auth: {
        user: this.configService.get('EMAIL_USERNAME'),
        pass: this.configService.get('EMAIL_PASSWORD'),
      },
    });
  }

  async sendEmail(to: string[], subject: string, text?: string, html?: string) {
    const info = await this.transport.send({
      from: {
        name: '裕业白事铺',
        address: 'mengkeys@hotmail.com',
      },
      to: to.join(','),
      subject,
      text,
      html,
    });
    console.log('Email sent: %s', info.messageId);
    return info;
  }
}
